package com.pj.lib.auraServer.exceptions;

import java.io.IOException;
import java.sql.SQLException;

import com.pj.lib.auraServer.components.DB.DB;
import com.pj.lib.auraServer.components.connection.ClientThread;
import com.pj.lib.auraServer.utils.Log;

public class AuraServerException extends Exception{

	public AuraServerException(String string) {
		super(string);
	}

	@Override
	public String getMessage() {
		return super.getMessage();
	}
	
	/**
	 * This function is for handling {@link AuraServerException} in components.
	 * @param e {@link AuraServerException} - fired exception
	 * @param o Object in which this exception has been fired
	 * @param ct {@link ClientThread} - client to who the exception message send
	 * @param function Name of function {@link AuraProtocol}
	 * @param rollbackTransaction When in transaction it rollbacking this transaction
	 * @param db Instance of DB - needed only when rollbacking transation - in other case it can be null
	 */
	public static void handleExceptionInComponent(Exception e, Object o, ClientThread ct, String function, Boolean rollbackTransaction, DB db){
		if(!rollbackTransaction) {
			Log.s(o, Log.getStackTrace(e));
			try {
				ct.handleSendServerError(function);
			} catch (IOException e1) {
				Log.s(o, Log.getStackTrace(e1));
			}
		} else {
			Log.s(o, Log.getStackTrace(e));
			try {
				db.transactionRollback();
				ct.handleSendServerError(function);
			} catch (SQLException e1) {
				Log.s(o, Log.getStackTrace(e1));
				try {
					ct.handleSendServerError(function);
				} catch (IOException e2) {
					Log.s(o, Log.getStackTrace(e2));
				}
			} catch (IOException e2) {
				Log.s(o, Log.getStackTrace(e2));
			}
		}
	}
	
}
